package com.xudow.app.util;

import java.util.Calendar;
import java.util.Date;
import java.util.Locale;

public class WeekCalendar {

    private static final int WEEK_DAYS_SIZE = 7;

    private int week;
    private int year;
    private int month;

    public WeekCalendar(int year, int month, int week) {
        this.year = year;
        this.month = month - 1;
        this.week = week;
    }

    public int[] getWeekDays() {
        int[] weekDays = new int[WEEK_DAYS_SIZE];

        Calendar calendar = Calendar.getInstance();
        calendar.setFirstDayOfWeek(Calendar.MONDAY);
        calendar.set(Calendar.YEAR, year);
        calendar.set(Calendar.MONTH, month);
        calendar.set(Calendar.DAY_OF_MONTH, 1);

        while (calendar.get(Calendar.DAY_OF_WEEK) != Calendar.MONDAY) {
            calendar.add(Calendar.DATE, -1);
        }

        for (int i = 0; i < (week - 1) * 7; i++) {
            calendar.add(Calendar.DATE, 1);
        }

        for (int i = 0; i < 7; i++) {
            weekDays[i] = calendar.get(Calendar.DAY_OF_MONTH);
            calendar.add(Calendar.DATE, 1);
        }

        return weekDays;
    }

    public String[] getWeekDaysWithMonth() {
        String[] weekDays = new String[WEEK_DAYS_SIZE];

        Calendar calendar = Calendar.getInstance();
        calendar.setFirstDayOfWeek(Calendar.MONDAY);
        calendar.set(Calendar.YEAR, year);
        calendar.set(Calendar.MONTH, month);
        calendar.set(Calendar.DAY_OF_MONTH, 1);

        while (calendar.get(Calendar.DAY_OF_WEEK) != Calendar.MONDAY) {
            calendar.add(Calendar.DATE, -1);
        }

        for (int i = 0; i < (week - 1) * 7; i++) {
            calendar.add(Calendar.DATE, 1);
        }

        for (int i = 0; i < 7; i++) {
            weekDays[i] = (calendar.get(Calendar.MONTH) + 1) + "-" + calendar.get(Calendar.DAY_OF_MONTH);
            calendar.add(Calendar.DATE, 1);
        }

        return weekDays;
    }

    public String[] getWeekDaysWithMonthYear() {
        String[] weekDays = new String[WEEK_DAYS_SIZE];

        Calendar calendar = Calendar.getInstance();
        calendar.setFirstDayOfWeek(Calendar.MONDAY);
        calendar.set(Calendar.YEAR, year);
        calendar.set(Calendar.MONTH, month);
        calendar.set(Calendar.DAY_OF_MONTH, 1);

        while (calendar.get(Calendar.DAY_OF_WEEK) != Calendar.MONDAY) {
            calendar.add(Calendar.DATE, -1);
        }

        for (int i = 0; i < (week - 1) * 7; i++) {
            calendar.add(Calendar.DATE, 1);
        }

        for (int i = 0; i < 7; i++) {
            weekDays[i] = calendar.get(Calendar.YEAR)
                    +
                    "-"
                    +
                    (calendar.get(Calendar.MONTH) + 1)
                    +
                    "-"
                    + calendar.get(Calendar.DAY_OF_MONTH);
            calendar.add(Calendar.DATE, 1);
        }

        return weekDays;
    }
}
